import re
from django.http import HttpResponse, JsonResponse
from rest_framework.response import Response
from django.forms import model_to_dict
from drf_yasg import openapi


def m_to_d(data):
    return model_to_dict(data)


def info(data):
    if isinstance(data, list) or isinstance(data, dict):
        return HttpResponse(JsonResponse(data, safe=False))
    else:
        return HttpResponse(data)
 

def success(data={}, code=1, msg='success', **args):
    if isinstance(data, list):
        data = change_key_for_list(data)
    if isinstance(data, dict):
        data = change_key_for_dict(data)
    res = {
        **args,
        "code": code,
        "msg": msg,
        "data": data,
    }

    # return HttpResponse(JsonResponse(res, safe=False))
    return Response(res)


def fail(msg='error', code=0):
    res = {
        "code": code,
        "msg": msg
    }
    return HttpResponse(JsonResponse(res, safe=False))


# 根据model还获取请求参数
def get_params_by_model(request, model):
    data = {}
    if request.method == 'GET':
        get = request.GET.get
    else:
        get = request.POST.get
    for filed in model._meta.get_fields():
        value = get(filed.name)
        if value:
            data[filed.name] = value
    return data


def get_param(request, key, default=None):
    if request.method == 'GET':
        get = request.GET.get
    else:
        get = request.POST.get
    try:
        value = get(key)
        if value:
            return value
        else:
            return default
    except TypeError as e:
        return None


def get_param_all(request, list_data):
    res_data = {}
    for i in list_data:
        res_data[i] = get_param(request, i)
    return res_data


def echo(**params):
    print(params)


""" 驼峰和下划线互转 """


# def to_camel_case(x):
#     """转驼峰法命名"""
#     return re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)


# def to_upper_camel_case(x):
#     """转大驼峰法命名"""
#     s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
#     return s[0].upper() + s[1:]


def to_lower_camel_case(x):
    """转小驼峰法命名"""
    s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
    return s[0].lower() + s[1:]


def to_underline(x):
    """转下划线命名"""
    return re.sub('(?<=[a-z])[A-Z]|(?<!^)[A-Z](?=[a-z])', '_\g<0>', x).lower()

# 把字典中的驼峰都变成下划线


def change_key_to_line(data):
    res_data = {}
    for key in data:
        new_key = to_underline(key)
        res_data[new_key] = data[key]
    return res_data

# 把返回数据的下划线换成小驼峰


def change_key_for_dict(data):
    res_data = {}
    for key in data:
        new_key = to_lower_camel_case(key)
        res_data[new_key] = data[key]
    return res_data


def change_key_for_list(data):
    new_data = []
    for item in data:
        obj = change_key_for_dict(item)
        new_data.append(obj)
    return new_data

# [('参数名称','参数描述','类型','是否必须')]
def get_schema_prams(list_data):
    manual_parameters = []
    for tup in list_data:
        obj = openapi.Parameter(tup[0], openapi.IN_QUERY, required=tup[3] or False,
                                description=tup[1], type=tup[2])
        manual_parameters.append(obj)

    return manual_parameters
